#include "types.h"
#include "matrix.h"
#include "tables.h"
#include "field.h"
#include "pht.h"
#include "sbox.h"

#include <string.h>

extern byte gf_256_169_mul(byte,byte);
extern word join_word(byte*);
extern void split_word(word, byte* ,int);

void one_round(byte* left_part, 
					byte* right_part, 
					word* sub_key,
					byte* des)
{
	int i;
	byte tmp[BLOCK_SIZE/2];
	memcpy(tmp, left_part, sizeof(tmp));
	for(i = 0; i < BLOCK_SIZE/2; ++i)
	{
		tmp[i] = sbox[tmp[i]];
	}
	
	byte matrix_tmp[4];

	byte_matrix_mul(mds, 4, 4, tmp, 4, 1, matrix_tmp, sizeof(matrix_tmp), element_add, gf_256_169_mul);
	word l0 = join_word(matrix_tmp);

	byte_matrix_mul(mds, 4, 4, tmp + 4, 4, 1, matrix_tmp, sizeof(matrix_tmp), element_add, gf_256_169_mul);
	word l1 = join_word(matrix_tmp);

	pht(l0, l1, &l0, &l1);

	l0 = l0 + sub_key[0];
	l1 = l1 + sub_key[1];

	split_word(l0, tmp, 4);
	split_word(l1, tmp+4, 4);

	for(i = 0; i < sizeof(tmp); ++i)
	{
		tmp[i] ^= right_part[i];
	}
	memcpy(des, tmp, sizeof(tmp));
}
